<?php

namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;

require_once('Common.php');
require_once('Imessage.php');

/**
 * 客服消息组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class CustomMessage extends Common implements Imessage
{
    /** 消息回复类型 */
    const MSGTYPE_MUSIC = 'music';
    const MSGTYPE_MP_NEWS = 'mpnews';
    const MSGTYPE_WXCARD = 'wxcard';


    const CUSTOM_MSG_RECORD_LIST = '/customservice/msgrecord/getmsglist?';
    const CUSTOM_SEND_URL = '/message/custom/send?';
    private $touser;
    private $message;

    function __construct($appid, $secret, $token)
    {
        parent::__construct($appid, $secret, $token);
    }
    function __destruct()
    {
    }

    /**
     * 获取聊天记录
     * @param type $data
     * @return boolean
     */
    public function getRecordMessage($data)
    {
        $this->throwAccessTokenError();
        $result = $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CUSTOM_MSG_RECORD_LIST . "access_token={$this->access_token}", Tools::json_encode($data));
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 设置接收信息用户
     * @param type $openid
     * @return type
     */
    public function setTouser($openid)
    {
        $this->touser = $openid;
        return $this;
    }

    /**
     * 获取接收信息的用户
     * @return type
     */
    public function getToUser()
    {
        return $this->touser;
    }

    /**
     * 发送文本消息
     * @param type $text 文本消息内容
     * @return array
     */
    public function text($text)
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_TEXT,
            'text' => array(
                'content' => $text,
            ),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送图片消息
     * @param type $media_id 发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
     * @return type
     */
    public function image($media_id = '')
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_IMAGE,
            'image' => array(
                'media_id' => $media_id,
            ),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送语音消息
     * @param type $media_id 发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
     * @return type
     */
    public function voice($media_id = '')
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_VOICE,
            'voice' => array(
                'media_id' => $media_id
            ),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送视频消息
     * @param type $media_id 发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
     * @param type $thumb_media_id 缩略图的媒体ID
     * @param type $title 图文消息/视频消息/音乐消息的标题
     * @param type $description 图文消息/视频消息/音乐消息的描述
     *  'video'        => array(
     * 'media_id'     => $media_id,
     * 'thumb_media_id'  => $thumb_media_id,
     * 'title' => $title,
     * 'description'=> $description,
     * ),
     * @return type
     */
    public function video($video)
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_VIDEO,
            'video' => $video,
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送音乐消息
     * @param type $title 图文消息/视频消息/音乐消息的标题
     * @param type $description 图文消息/视频消息/音乐消息的描述
     * @param type $musicurl 音乐链接
     * @param type $hqmusicurl 高品质音乐链接，wifi环境优先使用该链接播放音乐
     * @param type $thumb_media_id 缩略图的媒体ID
     * array(
     * 'title'       => $title,
     * 'description' => $description,
     * 'musicurl'    => $musicurl,
     * 'hqmusicurl'  => $hqmusicurl,
     * 'thumb_media_id'  => $thumb_media_id
     * )
     * @return array
     */
    public function music($music)
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_MUSIC,
            'music' => $music,
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送图文消息（点击跳转到外链） 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
     * @param type $newsData
     * @return type  "news":{
     * "articles": [
     * {
     * "title":"Happy Day",
     * "description":"Is Really A Happy Day",
     * "url":"URL",
     * "picurl":"PIC_URL"
     * },
     * {
     * "title":"Happy Day",
     * "description":"Is Really A Happy Day",
     * "url":"URL",
     * "picurl":"PIC_URL"
     * }
     * ]
     */
    public function news($news = array())
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_NEWS,
            'news' => array('articles' => $news),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送图文消息（点击跳转到图文消息页面） 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
     * @param type $media_id 发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
     * @return type
     */
    public function mpnews($media_id)
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_MP_NEWS,
            'mpnews' => array(
                'media_id' => $media_id
            ),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 发送卡券
     * @param type $card_id
     * @return type
     */
    public function wxcard($card_id)
    {
        $msg = array(
            'touser' => $this->getToUser(),
            'msgtype' => self::MSGTYPE_WXCARD,
            'wxcard' => array(
                'card_id' => $card_id
            ),
        );
        $this->Message($msg);
        return $this;
    }

    /**
     * 设置发送消息
     * @param string|array $msg 消息数组
     * @param bool $append 是否在原消息数组追加
     * @return array
     */
    public function message($msg = '', $append = false)
    {
        if (is_null($msg)) {
            $this->message = array();
        } elseif (is_array($msg)) {
            if ($append) {
                $this->message = array_merge($this->message, $msg);
            } else {
                $this->message = $msg;
            }
            return $this->message;
        }
        return $this->message;
    }

    /**
     * 发送客服消息
     * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
     * @return bool|array
     */
    public function send($send = true, $message = array())
    {
        $this->throwAccessTokenError();
        if (empty($message)) {
            if (empty($this->message)) {   //防止不先设置回复内容，直接调用reply方法导致异常
                return false;
            }
            $message = $this->message;
        }
        if (!$send) {
            return $message;
        }
        $result = Tools::httpPost(self::API_URL_PREFIX . self::CUSTOM_SEND_URL . "access_token={$this->access_token}", Tools::json_encode($message));
        $result = $this->parsing($result);
        return $result;
    }

}

?>